package vn.conghuy.talkwithme.responsor.responsor;

import vn.conghuy.talkwithme.R;
import vn.conghuy.talkwithme.data.AppPackageInfo;
import vn.conghuy.talkwithme.data.sqlmodel.SqlData;
import vn.conghuy.talkwithme.responsor.action.OpenAppAction;
import vn.conghuy.talkwithme.utils.StringWrapper;
import vn.conghuy.talkwithme.utils.Utils;
import android.content.Context;

public class OpenAppResponsor extends Responsor {
	
	AppPackageInfo[] appPackageInfos;
	
	public OpenAppResponsor(Context context) {
		super(context);
		
		appPackageInfos = new AppPackageInfo[SqlData.appPackage.length];
		
		//load PackageInfo
		for(int i = 0; i < SqlData.appPackage.length; i++){
			
			String appPackageStr = SqlData.appPackage[i];
			String[] elements = appPackageStr.split("\\|");
			AppPackageInfo packageInfo = new AppPackageInfo();
			
			for(int j = 0; j < elements.length; j++){
				
				String element = elements[j];
				
				if(j != elements.length - 1){					//is keyword
					packageInfo.insertKeyword(element);
				}
				else{											//is package name
					packageInfo.setPackageName(element);
				}
			}
			
			if(packageInfo.isValid()){
				appPackageInfos[i] = packageInfo;
			}
		}
	}
	
	@Override
	public String response(String request) {
		// kiem tra xem co stop khong
		if(Utils.isExistStringInArray(SqlData.stopWords, request) == true){
			relativeLastResponse = RELATIVE_NONE;
			return Utils.getRandomString(SqlData.callCancelPhrases);
		}
		// kiem tra xem co verb khong. neu co thi xu ly tiep
		StringWrapper openAppVerbWrapper = new StringWrapper();
		if(Utils.isExistElemInArrayIsWordOfStr(SqlData.openAppVerb, request, openAppVerbWrapper)){
			// lay appPhrase.
			String openAppVerb = openAppVerbWrapper.getStr();
			int appPhraseIndex = Utils.indexOfWord(request, openAppVerb);
			String appPhrase = request.substring(appPhraseIndex + openAppVerb.length() + 1);
			// kiem tra appPhrase, neu ton tai keyword (lan luot trong mang appInfo) thi mo bang package name co trong package info
			for(AppPackageInfo appPackageInfo:appPackageInfos){
				if(appPackageInfo.isExistKeywordBeWordInPhrase(appPhrase)){
					OpenAppAction openAppAction = new OpenAppAction(context);
					boolean result = openAppAction.launchIntent(appPackageInfo.getPackageName());
					if(result == false){
						result = openAppAction.launchPackage(appPackageInfo.getPackageName());
						// TODO: replace hard code by data in sql
						if(result == false) return "You have not installed that application";
					}
					return context.getString(R.string.open_app_command);
				}
			}
		}
		
		return super.response(request);
	}
	
	@Override
	public int indexOfMainVerb(String request) {
		StringWrapper openAppVerbWrapper = new StringWrapper();
		if(Utils.isExistElemInArrayIsWordOfStr(SqlData.openAppVerb, request, openAppVerbWrapper))
			return Utils.indexOfWord(request, openAppVerbWrapper.getStr());
		else return super.indexOfMainVerb(request);
	}
}
